{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test of the function, get_parent_missingness_pairs()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    " \n",
    "path = os.getcwd()\n",
    "path = os.path.abspath(os.path.join(path, os.pardir))\n",
    "sys.path.append(path)\n",
    "import causallearn.search.ConstraintBased.PC as pcp\n",
    "import causallearn.utils.cit as cit\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Generate data with missing values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "sz = 1000\n",
    "data = np.zeros((sz, 4))\n",
    "\n",
    "X = np.random.normal(0, 1.0, size=sz)\n",
    "Z = 2 * X + 0.5 * np.random.normal(0, 1.0, size=sz)\n",
    "Y = 0.5 * Z + 0.5 * np.random.normal(0, 1.0, size=sz)\n",
    "W = 0.2 * X + 0.8 * Y + 0.5 * np.random.normal(0, 1.0, size=sz)\n",
    "U = np.random.normal(0, 1.0, size=sz)\n",
    "data[:, 0], data[:, 1], data[:, 2], data[:, 3] = X, Y, Z, W\n",
    "mdata = data.copy()\n",
    "\n",
    "# X--> Z -->Y\n",
    "# X--> W <--Y\n",
    "# W --> Rx\n",
    "# Z --> Ry\n",
    "\n",
    "mdata[W > 0, 0] = np.nan\n",
    "mdata[Z > 0, 1] = np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>X</th>\n",
       "      <th>Y</th>\n",
       "      <th>Z</th>\n",
       "      <th>W</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.592416</td>\n",
       "      <td>-1.074040</td>\n",
       "      <td>-2.342733</td>\n",
       "      <td>-2.400372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.355830</td>\n",
       "      <td>0.802140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.798158</td>\n",
       "      <td>0.835962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.096105</td>\n",
       "      <td>0.664958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1.002755</td>\n",
       "      <td>-0.753031</td>\n",
       "      <td>-2.732670</td>\n",
       "      <td>-0.530232</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>-0.196136</td>\n",
       "      <td>0.028282</td>\n",
       "      <td>-0.173904</td>\n",
       "      <td>-0.283147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>-0.158138</td>\n",
       "      <td>-1.430875</td>\n",
       "      <td>-0.057203</td>\n",
       "      <td>-1.233116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.171953</td>\n",
       "      <td>-0.282680</td>\n",
       "      <td>0.019390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>-0.332086</td>\n",
       "      <td>0.007037</td>\n",
       "      <td>-0.921206</td>\n",
       "      <td>-0.297697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.388260</td>\n",
       "      <td>-1.481877</td>\n",
       "      <td>0.267360</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            X         Y         Z         W\n",
       "0   -0.592416 -1.074040 -2.342733 -2.400372\n",
       "1         NaN       NaN  2.355830  0.802140\n",
       "2         NaN       NaN  1.798158  0.835962\n",
       "3         NaN       NaN  1.096105  0.664958\n",
       "4   -1.002755 -0.753031 -2.732670 -0.530232\n",
       "..        ...       ...       ...       ...\n",
       "995 -0.196136  0.028282 -0.173904 -0.283147\n",
       "996 -0.158138 -1.430875 -0.057203 -1.233116\n",
       "997       NaN -0.171953 -0.282680  0.019390\n",
       "998 -0.332086  0.007037 -0.921206 -0.297697\n",
       "999       NaN  0.388260 -1.481877  0.267360\n",
       "\n",
       "[1000 rows x 4 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.DataFrame(mdata, columns = ['X','Y','Z','W'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Test the correctness of the function\n",
    "* Index 3 is the direct cause of the missingness indicator of index 0;  \n",
    "* Index 2 is the direct cause of the missingness indicator of index 1;  \n",
    "* The correct result should be {'prt':[array(3),array(2)], 'm': [0, 1]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'prt': [array([3]), array([2])], 'm': [0, 1]}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pcp.get_parent_missingness_pairs(mdata, 0.05, indep_test=cit.mv_fisherz)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# End"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.3 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
